using Godot;
using Godot.Collections;
using System;
//using System.IO;

public partial class TaskManager : Node2D
{
	private MessageManager _messageManager;
	public Array<CollectTask> collectTasks = new Array<CollectTask>();
	public Array<ReachTask> reachTasks = new Array<ReachTask>();
	public Array<TalkTask> talkTasks = new Array<TalkTask>();
	public Array<ParentTask> parentTasks = new Array<ParentTask>();
	public Array<Tasks> runningTasks = new Array<Tasks>();
	public Array<Tasks> completedTasks = new Array<Tasks>();
	public Dictionary<ParentTask, Array<Tasks>> tasksDic = new Dictionary<ParentTask,Array<Tasks>>();
	public Tasks FollowingTask;
	public Dictionary<string,Variant> tasksText;
	public void Initialize()
	{
		foreach(Tasks task in GetChildren())
		{
			if(task.follow)
			{
				FollowingTask = task;
				break;
			}
		}
		
		var Game = GetNode<Game>("/root/Game");
		_messageManager = Game.get_message_manager();
		_messageManager.MapAdd += AddReachTasks;
		AcceptTask("main_1");
		AcceptTask("main_1.1");
		tasksText = LoadTask();
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public void SortTasks(Tasks task)
	{
		if(task.CurrentType == Tasks.Type.COLLECT)
		{
			collectTasks.Add(task as CollectTask);
		}
		else if(task.CurrentType == Tasks.Type.REACH)
		{
			reachTasks.Add(task as ReachTask);
		}
		else if(task.CurrentType == Tasks.Type.TALK)
		{
			talkTasks.Add(task as TalkTask);
		}
		else if(task.CurrentType == Tasks.Type.PARENT)
		{
			parentTasks.Add(task as ParentTask);
			tasksDic.Add(task as ParentTask ,new Array<Tasks>());
		}

		if(task.CurrentState == Tasks.State.WAITING)
		{
			runningTasks.Add(task);
		}
		else
		{
			completedTasks.Add(task);
		}
		
		if(task.ParentTask ==null)
		{return;}
		foreach(ParentTask parent in tasksDic.Keys)
		{
			if(task.ParentTask == parent.name)
			{
				tasksDic[parent].Add(task);
				break;
			}
		}

	}

	public void AcceptTask(string taskName)
	{
		if(CheckTasks(taskName))
		{
			return;
		}
		var Game = GetNode<Game>("/root/Game");
		string path = "res://resources/tasks/" + taskName + ".tscn";
		Tasks NewTask = Game.InstantScene(path) as Tasks;
		NewTask.CurrentState = Tasks.State.WAITING;
		ChangeFollow(NewTask);
		this.AddChild(NewTask);
		SortTasks(NewTask);
		_messageManager.EmitSignal("TaskAccept",taskName);
	}

	public void AddReachTasks(Map map)
	{
		GD.Print("添加地图",map.Name);
		if(map.Name == "Scp7210")
		{
			AcceptTask("tree7210");
			AcceptTask("tree7210_1.1");
		}
	}

	public Dictionary<string,Variant> LoadTask()
	{
		Dictionary<string,Variant> tasks = new Dictionary<string,Variant>();
		string path = "res://tasks.json";
		if(!FileAccess.FileExists(path))
		{return null;}
		var file = FileAccess.Open(path,FileAccess.ModeFlags.Read);
		var text = file.GetAsText();
		tasks = Json.ParseString(text).AsGodotDictionary<string,Variant>();
		return tasks;
	}
	public void ChangeFollow(Tasks newTask)
	{
		if(FollowingTask != null)
		{
			FollowingTask.follow = false;
		}
		newTask.follow = true;
		FollowingTask = newTask;
	}

	public bool CheckTasks(string taskName)
	{
		bool IsHave = false;
		foreach(var child in this.GetChildren())
		{
			Tasks task = child as Tasks;
			if(task.taskID == taskName)
			{
				GD.Print("任务已接取");
				IsHave = true;
				break;
			}
		}
		return IsHave; 
	}
}
